﻿using Levfier.Proccessors.Parameters;
using System.Windows;
using System.Windows.Controls;

namespace Levfier.GUI.EditorControls.Parameters
{
    /// <summary>
    /// ノブコントロール生成のイベント
    /// </summary>
    /// <param name="knob"></param>
    public delegate void CreateKnobControl(KnobControl knob);

    /// <summary>
    /// KnobControlCreateWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class KnobControlCreateWindow : Window
    {
        /// <summary>
        /// ノブコントロール生成イベント
        /// </summary>
        public CreateKnobControl Create;

        /// <summary>
        /// 設定対象のパラメータ
        /// </summary>
        private ProccessParameter param;

        /// <summary>
        /// 最大値設定用のパラメータ
        /// </summary>
        private ProccessParameter maxParam;

        /// <summary>
        /// 最小値設定用のパラメータ
        /// </summary>
        private ProccessParameter minParam;

        /// <summary>
        /// ノブコントロールを設定する対象のパネル
        /// </summary>
        private Panel addTargetPanel;

        /// <summary>
        /// コンストラクタ
        /// </summary>
        public KnobControlCreateWindow(ProccessParameter param, Panel panel)
        {
            InitializeComponent();
            this.paramName.Text = param.Name;

            this.param = param;
            ParameterSlider maxSlider = new ParameterSlider();
            ParameterSlider minSlider = new ParameterSlider();

            //最大値設定用のスライダを追加
            this.maxParam = new ProccessParameter();
            maxParam.Name = "Max";
            maxParam.MaxValue = param.MaxValue;
            maxParam.Value = param.MaxValue;
            maxParam.MinValue = param.MinValue;
            maxParam.Step = param.Step;
            maxSlider.RegisterParam(maxParam);
            this.stack.Children.Add(maxSlider);

            //最小値設定用のスライダを追加
            this.minParam = new ProccessParameter();
            minParam.Name = "Min";
            minParam.MaxValue = param.MaxValue;
            minParam.Value = param.MinValue;
            minParam.MinValue = param.MinValue;
            minParam.Step = param.Step;
            minSlider.RegisterParam(minParam);
            this.stack.Children.Add(minSlider);

            this.addTargetPanel = panel;
        }

        /// <summary>
        /// ノブコントロール生成
        /// ウィンドウを閉じる
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Create_Click(object sender, RoutedEventArgs e)
        {
            if (this.maxParam.Value > this.minParam.Value)
            {
                KnobControl knob = new KnobControl();
                knob.MaxValue = this.maxParam.Value;
                knob.MinValue = this.minParam.Value;
                knob.paramName.Text = this.paramName.Text;
                knob.RegisterParam(this.param);
                knob.Delete += Knob_Delete;
                this.addTargetPanel.Children.Add(knob);
                this.Close();
            }
            else
            {
                MessageBox.Show("最大値、最小値の設定が不正です。", "パラメータ生成エラー", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// ウィンドウを閉じる
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// ノブ削除
        /// </summary>
        /// <param name="sender"></param>
        private void Knob_Delete(object sender)
        {
            this.addTargetPanel.Children.Remove(sender as System.Windows.UIElement);
        }
    }
}
